home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Views / Standard Views / Progress.cp < prev    next >
Encoding:
Text File  |  1997-06-28  |  1.7 KB  |  115 lines  |  [TEXT/CWIE]

  1. // Progress.cp
  2.  
  3. #ifndef Progress_h
  4. #include "Progress.h"
  5. #endif
  6.  
  7. Progress::Progress()
  8.   : portion( 0 ),
  9.      total( maxuint32 ),
  10.      totalKnown( false )
  11.   {
  12.   }
  13.  
  14. Progress::Progress( uint32 theTotal )
  15.   : portion( 0 ),
  16.      total( theTotal ),
  17.      totalKnown( true )
  18.   {
  19.   }
  20.  
  21. void Progress::Clear()
  22.   {
  23.     bool changed = totalKnown;
  24.     
  25.     totalKnown = false;
  26.     total = maxuint32;
  27.     portion = 0;
  28.     
  29.     if ( changed )
  30.         Announce();
  31.   }
  32.  
  33. void Progress::Clear( uint32 newTotal )
  34.   {
  35.     bool changed = !totalKnown || portion > 0;
  36.     
  37.     totalKnown = true;
  38.     total = newTotal;
  39.     portion = 0;
  40.     
  41.     if ( changed )
  42.         Announce();
  43.   }
  44.  
  45. void Progress::SetTotal( uint32 newTotal )
  46.   {
  47.     Assert( portion <= newTotal );
  48.  
  49.     if ( totalKnown && total == newTotal )
  50.         return;
  51.     
  52.     bool changed = !totalKnown || portion > 0;
  53.     
  54.     totalKnown = true;
  55.     total = newTotal;
  56.  
  57.     if ( changed )
  58.         Announce();
  59.   }
  60.  
  61. void Progress::SetPortion( uint32 newPortion )
  62.   {
  63.     Assert( newPortion <= total );
  64.  
  65.     if ( newPortion == portion )
  66.         return;
  67.     
  68.     portion = newPortion;
  69.     Announce();
  70.   }
  71.  
  72. void Progress::Set( uint32 newPortion, uint32 newTotal )
  73.   {
  74.     Assert( newPortion <= newTotal );
  75.     
  76.     if ( !totalKnown )
  77.       {
  78.         totalKnown = true;
  79.         total = newTotal;
  80.         portion = newPortion;
  81.         Announce();
  82.         return;
  83.       }
  84.     
  85.     bool changed = true;
  86.     
  87.     if ( portion == 0 && newPortion == 0 )
  88.         changed = false;
  89.     
  90.     if ( portion == total && newPortion == newTotal )
  91.         changed = false;
  92.     
  93.     if ( portion == newPortion && total == newTotal )
  94.         changed = false;
  95.     
  96.     portion = newPortion;
  97.     total = newTotal;
  98.  
  99.     if ( changed )
  100.         Announce();
  101.   }
  102.  
  103. void Progress::operator++()
  104.   {
  105.     Assert( !Finished() );
  106.     portion++;
  107.     Announce();
  108.   }
  109.  
  110. double Progress::Fraction() const
  111.   {
  112.     Assert( totalKnown );
  113.     return double( portion ) / double( total );
  114.   }
  115.